package utils

import (
	"fmt"
	"gitee.com/captials-team/ubdframe/src/common/consts"
	"strings"
	"time"
)

// RandFigureCode 纯数字
func RandFigureCode(l int) string {
	return RandCode(l, strings.Split(consts.Numbers, ","))
}

// RandLetterCode 纯字母（大小写都包含）
func RandLetterCode(l int) string {
	return RandCode(l, strings.Split(consts.Letters, ","))
}

// RandLetterFigureCode 字母（大小写都包含）+数字
// 如需纯大写或纯小写，自行转换 strings.ToLower
func RandLetterFigureCode(l int) string {
	b := strings.Split(consts.Numbers, ",")
	a := strings.Split(consts.Letters, ",")

	return RandCode(l, append(a, b...))
}

func RandCode(l int, chars []string) string {
	code := ""

	max := int64(len(chars))

	for i := 1; i <= l; i++ {
		//index, _ := rand.Int(rand.Reader, big.NewInt(max))
		index := TrueScopeRand(0, max) //真随机数
		code += fmt.Sprintf("%s", chars[index])
	}

	return code
}

// RandNickname 随机昵称
func RandNickname() string {
	return fmt.Sprintf("user-%s", RandLetterFigureCode(8))
}

// RandOrderNoByDateTime 按日期生成单据编号，类似： TX202404230714209992
func RandOrderNoByDateTime(prefix string, useDate bool, useTime bool, randLen int) string {
	no := prefix
	if useDate {
		no += time.Now().Format("20060102")
	}
	if useTime {
		no += time.Now().Format("150405")
	}
	if randLen > 0 {
		no += RandFigureCode(randLen)
	}
	return no
}

// GetAccessKey 获取一个accessKey，通常为20个字符
func GetAccessKey() string {
	return RandLetterFigureCode(20)
}

// GetAccessSecret 获取一个accessSecret，通常为30个字符
func GetAccessSecret() string {
	return RandLetterFigureCode(30)
}
